<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/12/21 09:30:59 $)
// $RCSfile: functions_clientdl.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Build the nav link
*/
function buildDownloadNavLink($nav)
{
	global $dbCore;

	$categorybits = explode(",", $nav);
	if (!count($categorybits))
	{
		return;
	}

	$_categoryidlist = array();
	foreach ($categorybits as $key=>$val)
	{
		if (is_numeric($val) && !empty($val))
		{
			$_categoryidlist[] = intval($val);
		}
	}

	$_categorylist = array();
	$dbCore->query("SELECT `downloadcategoryid`, `title` FROM `". TABLE_PREFIX ."downloadcategories` WHERE `downloadcategoryid` IN (". buildIN($_categoryidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_categorylist[$dbCore->Record["downloadcategoryid"]] = $dbCore->Record["title"];
	}

	$_nav = "";
	$navbits = "0";
	foreach ($_categoryidlist as $key=>$val)
	{
		$_nav .= ' &raquo; <a href="index.php?_m=downloads&_a=view&parentcategoryid='. intval($val) .'&nav='. urlencode($navbits) .'" id="navlink">'.$_categorylist[$val]."</a>";
		$navbits .= ",".intval($val);
	}

	return $_nav;
}

/**
* Check the category against the current user group
*/
function checkDownloadCatUserGroup($downloadcategoryid)
{
	global $_SWIFT, $dbCore;

	// Always return true if its the main category
	if ($downloadcategoryid == 0)
	{
		return true;
	}

	$_downloadcategory = $dbCore->queryFetch("SELECT downloadcategories.* FROM `". TABLE_PREFIX ."downloadcategories` AS downloadcategories LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (downloadcategories.downloadcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DOWNLOADS ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND downloadcategories.downloadcategoryid = '". intval($downloadcategoryid) ."' LIMIT 1;");
	if (!empty($_downloadcategory["downloadcategoryid"]))
	{
		return $_downloadcategory;
	}

	return false;
}

/**
* Gets a list of sub categories from the parent category
*/
function getSubCategories($downloadcategoryid)
{
	global $dbCore, $_SWIFT, $mimelist;

	$categorylist = $categoryidlist = $splitcatlist = array();
	$index = $splitindex = 0;
	if ($_SWIFT["settings"]["dl_catdisplayorder"] == 4)
	{
		$orderfield = "totalitems";
	} else if ($_SWIFT["settings"]["dl_catdisplayorder"] == 3) {
		$orderfield = "downloadcategoryid";
	} else if ($_SWIFT["settings"]["dl_catdisplayorder"] == 2) {
		$orderfield = "title";
	} else {
		$orderfield = "displayorder";
	}

	if ($_SWIFT["settings"]["dl_catorder"] == 2)
	{
		$ordertype = "DESC";
	} else {
		$ordertype = "ASC";
	}


	$dbCore->query("SELECT downloadcategories.* FROM `". TABLE_PREFIX ."downloadcategories` AS downloadcategories LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (downloadcategories.downloadcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DOWNLOADS ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND downloadcategories.parentcategoryid = '". intval($downloadcategoryid) ."' ORDER BY ". $dbCore->escape($orderfield) ." ". $dbCore->escape($ordertype) .";");
	while ($dbCore->nextRecord())
	{
		// Add only if the sub cat belongs to this user group
		if ($dbCore->Record["categorytype"] == SWIFTPUBLIC)
		{
			$catmod = $index%2;
			$categoryidlist[] = $dbCore->Record["downloadcategoryid"];
			$categorylist[$dbCore->Record["downloadcategoryid"]] = $dbCore->Record;
			$categorylist[$dbCore->Record["downloadcategoryid"]]["split"] = $catmod;
			$categorylist[$dbCore->Record["downloadcategoryid"]]["downloaditemlist"] = $categorylist[$dbCore->Record["downloadcategoryid"]]["downloaditemidlist"] = array();
			$lastitem = $dbCore->Record["downloadcategoryid"];

			if ($catmod == 0)
			{
				$splitcatlist[$splitindex][1] = &$categorylist[$dbCore->Record["downloadcategoryid"]];
			} else if ($catmod == 1) {
				$splitcatlist[$splitindex][2] = &$categorylist[$dbCore->Record["downloadcategoryid"]];
				$splitindex++;
			}
			$index++;
		}
	}

	if (isset($lastitem))
	{
		$categorylist[$lastitem]["split"] = 1;
	}
	unset($lastitem);

	// Add the parent category to the category list too
	$categoryidlist[] = $downloadcategoryid;

	// Once we have the list of categories under the parent category we itterate through the links and get all the articles under the specified categories
	if ($_SWIFT["settings"]["dl_itdisplayorder"] == 5) {
		$orderfield = "totaldownloads";
	} else if ($_SWIFT["settings"]["dl_itdisplayorder"] == 4) {
		$orderfield = "updatedateline";
	} else if ($_SWIFT["settings"]["dl_itdisplayorder"] == 3) {
		$orderfield = "downloaditemid";
	} else if ($_SWIFT["settings"]["dl_itdisplayorder"] == 2) {
		$orderfield = "title";
	} else {
		$orderfield = "displayorder";
	}

	if ($_SWIFT["settings"]["dl_itorder"] == 2)
	{
		$ordertype = "DESC";
	} else {
		$ordertype = "ASC";
	}

	$parentitemlist = $downloaditemlist = array();

	$itemlimit = $_SWIFT["settings"]["dl_maxcatarticles"];
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."downloaditems` AS downloaditems LEFT JOIN `". TABLE_PREFIX ."downloaddesc` AS downloaddesc ON (downloaditems.downloaditemid = downloaddesc.downloaditemid) WHERE downloaditems.downloadcategoryid IN (". buildIN($categoryidlist) .") ORDER BY downloaditems.`". $dbCore->escape($orderfield) ."` ". $dbCore->escape($ordertype) .";");
	while ($dbCore->nextRecord())
	{
		if (count($categorylist[$dbCore->Record["downloadcategoryid"]]["downloaditemidlist"]) >= $itemlimit && $dbCore->Record["downloadcategoryid"] != $downloadcategoryid || $itemlimit == 0)
		{
			continue;
		}

		$downloaditemlist[$dbCore->Record["downloaditemid"]] = $dbCore->Record;
		$extension = getFileExtension($dbCore->Record["filename"]);
		$mimedata = $mimelist[$extension];
		if (!empty($mimedata[1]))
		{
			$downloaditemlist[$dbCore->Record["downloaditemid"]]["icon"] = $mimedata[1];
		} else {
			$downloaditemlist[$dbCore->Record["downloaditemid"]]["icon"] = "mimeico_blank.gif";
		}

		$downloaditemlist[$dbCore->Record["downloaditemid"]]["description"] = strip_tags(substr($dbCore->Record["description"], 0, $_SWIFT["settings"]["dl_climit"]));
		$downloaditemlist[$dbCore->Record["downloaditemid"]]["filesize"] = formattedSize($dbCore->Record["filesize"]);
		$downloaditemlist[$dbCore->Record["downloaditemid"]]["lastmodified"] = edate($_SWIFT["settings"]["dt_datetimeformat"], iif(empty($dbCore->Record["updatedateline"]), $dbCore->Record["dateline"], $dbCore->Record["updatedateline"]));

		if (!empty($dbCore->Record["downloadcategoryid"]) && $dbCore->Record["downloadcategoryid"] != $downloadcategoryid)
		{
			$categorylist[$dbCore->Record["downloadcategoryid"]]["downloaditemlist"][] = $downloaditemlist[$dbCore->Record["downloaditemid"]];
			$categorylist[$dbCore->Record["downloadcategoryid"]]["downloaditemidlist"][] = $dbCore->Record["downloaditemid"];
		}

		if ($downloadcategoryid == $dbCore->Record["downloadcategoryid"])
		{
			$parentitemlist[] = $downloaditemlist[$dbCore->Record["downloaditemid"]];
		}
	}

	$funcresult = array();
	$funcresult["categorylist"] = $categorylist;
	$funcresult["downloaditemlist"] = $downloadarticlelist;
	$funcresult["parentitemlist"] = $parentitemlist;
	$funcresult["splitcatlist"] = $splitcatlist;

	return $funcresult;
}

/**
* Increases the download count
*/
function increaseDownloadCount($downloaditemid)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."downloaditems` SET `totaldownloads` = `totaldownloads`+1 WHERE `downloaditemid` = '". intval($downloaditemid) ."';");

	return true;
}
?>